/**
 * 订单管理API接口
 */
import request from '@/utils/request'
import type {
  Order,
  OrderType,
  OrderPayment,
  CreateOrderRequest,
  OrderListQuery,
  OrderListResponse,
} from '@/types/order'

/**
 * 创建订单
 */
export function createOrder(data: CreateOrderRequest) {
  return request({
    url: '/api/crm/order/create',
    method: 'post',
    data,
  })
}

/**
 * 获取订单列表
 */
export function getOrderList(params: OrderListQuery) {
  return request({
    url: '/api/crm/order/list',
    method: 'get',
    params,
  })
}

/**
 * 获取订单详情
 */
export function getOrderDetail(id: number) {
  return request<Order>({
    url: `/api/crm/order/detail/${id}`,
    method: 'get',
  })
}

/**
 * 更新订单
 */
export function updateOrder(id: number, data: Partial<Order>) {
  return request({
    url: `/api/crm/order/update/${id}`,
    method: 'put',
    data,
  })
}

/**
 * 删除订单
 */
export function deleteOrder(id: number) {
  return request({
    url: `/api/crm/order/delete/${id}`,
    method: 'delete',
  })
}

/**
 * 获取订单类型列表
 */
export function getOrderTypeList(params?: { category?: number; status?: number }) {
  return request<OrderType[]>({
    url: '/api/crm/order/type/list',
    method: 'get',
    params,
  })
}

/**
 * 根据订单大类获取类型
 */
export function getOrderTypesByCategory(category: number) {
  return request<OrderType[]>({
    url: `/api/crm/order/type/category/${category}`,
    method: 'get',
  })
}

/**
 * 新增支付记录
 */
export function addPaymentRecord(data: Partial<OrderPayment>) {
  return request({
    url: '/api/crm/order/payment/add',
    method: 'post',
    data,
  })
}

/**
 * 获取支付记录列表
 */
export function getPaymentList(orderId: number) {
  return request<OrderPayment[]>({
    url: '/api/crm/order/payment/list',
    method: 'get',
    params: { order_id: orderId },
  })
}

/**
 * 核实支付记录
 */
export function verifyPayment(id: number) {
  return request({
    url: `/api/crm/order/payment/verify/${id}`,
    method: 'post',
  })
}

/**
 * 提交订单审核
 */
export function submitOrderForApproval(id: number) {
  return request({
    url: `/api/crm/order/submit/${id}`,
    method: 'post',
  })
}

/**
 * 取消订单
 */
export function cancelOrder(id: number) {
  return request({
    url: `/api/crm/order/cancel/${id}`,
    method: 'post',
  })
}
